import wx
import datetime
import time

class ReminderDialog(wx.Dialog):
    def __init__(self, parent, title,
                pos=wx.DefaultPosition,
                size=wx.DefaultSize,
                style=wx.CAPTION,
                item=None,
                reminder_properties=None,
                reminder_active=False):
                 
        wx.Dialog.__init__(self, parent, -1, title, pos, (700,500))

        method,period,rdate,rtime = reminder_properties
        
        ###################### LABEL ################################################################
        
        label = "id: %s\nuuid: %s\n%s"%(item.id, item.uuid, item.name)
        item_txt1 = wx.StaticText(self, -1, label)
        font = wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD)
        item_txt1.SetFont(font)
        
        label = reminder_active and "Reminder is Active" or "Reminder is Not Active"
        item_txt2 = wx.StaticText(self, -1, label)
        font = wx.Font(14, wx.SWISS, wx.ITALIC, wx.BOLD)
        item_txt2.SetFont(font)
        if reminder_active:
            item_txt2.SetForegroundColour('red')
        
        #############################################################################################   
        
        ###################### METHOD ###############################################################

        self.method_list = ['email','sms','im']
        rb1 = wx.RadioBox(self, -1, "method", wx.DefaultPosition, wx.DefaultSize, self.method_list, 1, wx.RA_SPECIFY_COLS)             
        n = self.method_list.index(method)        
        rb1.SetSelection(n)
        #rb1.SetToolTip(wx.ToolTip("This is a ToolTip!"))
        self.Bind(wx.EVT_RADIOBOX, self.EvtRadioBox1, rb1)

        #############################################################################################   
           
        ###################### PERIOD ###############################################################  
        self.period_list = ['day','week','month','year']
        rb2 = wx.RadioBox(self, -1, "period", wx.DefaultPosition, wx.DefaultSize, self.period_list, 1, wx.RA_SPECIFY_COLS) #, 2, wx.RA_SPECIFY_COLS
        n = self.period_list.index(period)
        rb2.SetSelection(n)  
        #rb2.SetToolTip(wx.ToolTip("This is a ToolTip!"))
        self.Bind(wx.EVT_RADIOBOX, self.EvtRadioBox2, rb2)
        
        ############################################################################################# 
        
        ###################### CALENDAR #############################################################
        
        if period=='year':
            today = datetime.datetime.today()
            date = wx.DateTime()
            date.SetTimeT(time.mktime((today.year, int(rdate[:2]), int(rdate[2:]), 0, 0, 0, 0, 0, 0)))
        else:
            date = wx.DateTime_Now()        
        
        cal = wx.calendar.CalendarCtrl(self, -1, date, pos=wx.DefaultPosition, 
                             style = wx.calendar.CAL_SHOW_HOLIDAYS
                             | wx.calendar.CAL_SUNDAY_FIRST
                             | wx.calendar.CAL_SEQUENTIAL_MONTH_SELECTION)

        self.Bind(wx.calendar.EVT_CALENDAR, self.OnCalSelected, cal) #id=cal.GetId())
        self.Bind(wx.calendar.EVT_CALENDAR_SEL_CHANGED, self.OnCalSelected, cal)

        # Set up the control to display a set of holidays:
        self.Bind(wx.calendar.EVT_CALENDAR_MONTH, self.OnChangeMonth, cal)
        self.holidays = [(1,1), (10,31), (12,25) ]    # (these don't move around)
        
        #############################################################################################
               
        ###################### DAY OF WEEK ##########################################################
        self.day_list = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday', 'Sunday']
        # using datetime.weekday ---> Monday is 0 and Sunday is 6
        if period=='week':
            n = int(rdate)
        else:
            n = 0
        rb3 = wx.RadioBox(
                self, -1, "day of week", wx.DefaultPosition, wx.DefaultSize,
                self.day_list, 1, wx.RA_SPECIFY_COLS) #, 2, wx.RA_SPECIFY_COLS
        
        rb3.SetSelection(n)
        self.Bind(wx.EVT_RADIOBOX, self.EvtRadioBox3, rb3)

        ##############################################################################################  
        
        ###################### DAY OF MONTH ##########################################################

        if period=='month':
            i = int(rdate)
        else:
            i = 1
        sc_text = wx.StaticText(self, -1, "Day of Month", (45, 15))
        sc = wx.SpinCtrl(self, -1, "", (30, 100))
        sc.SetRange(1,31)
        sc.SetValue(i)
        self.Bind(wx.EVT_SPINCTRL, self.EvtSpincCtrl, sc)

        ###################### TIME ##################################################################          

        self.hour_list = ['12:00 AM', '11:00 PM', '10:00 PM', '9:00 PM', '8:00 PM', '7:00 PM', '6:00 PM', '5:00 PM', '4:00 PM', '3:00 PM', '2:00 PM', '1:00 PM',
                          '12:00 PM', '11:00 AM', '10:00 AM', '9:00 AM', '8:00 AM', '7:00 AM', '6:00 AM', '5:00 AM', '4:00 AM', '3:00 AM', '2:00 AM', '1:00 AM']
                     
        self.rtime_list = ['0000', '2300', '2200', '2100', '2000', '1900', '1800', '1700', '1600', '1500', '1400', '1300',
                           '1200', '1100', '1000', '0900', '0800', '0700', '0600', '0500', '0400', '0300', '0200', '0100']
                      
        if self.rtime_list.count(rtime):
            hour = self.rtime_list.index(rtime)
        else:
            hour = 0
            rtime = '0000'
            
        self.time_ctrl = wx.TextCtrl(self, -1, self.hour_list[hour], wx.DefaultPosition, (75, -1)) #(30, 50)
        h = self.time_ctrl.GetSize().height
        w = self.time_ctrl.GetSize().width + self.time_ctrl.GetPosition().x + 2
        
        self.spin = wx.SpinButton(self, -1,
                                  (w, 50),
                                  (h*2/3, h),
                                  wx.SP_VERTICAL|wx.SP_WRAP)
        self.spin.SetRange(0, 23)
        self.spin.SetValue(hour)

        self.Bind(wx.EVT_SPIN, self.OnSpin, self.spin)
        self.Bind(wx.EVT_TEXT, self.OnChangeTime, self.time_ctrl)

        ############################################################################################## 
         
        ##################### OK/CANCEL BUTTONS ######################################################          

        btn_ok = wx.Button(self, wx.ID_OK, "OK")
        btn_ok.SetDefault()
    
        btn_cancel = wx.Button(self, wx.ID_CANCEL, "CANCEL")

        ##############################################################################################  
                
        ##################### SIZERS #################################################################  
                
        H_sizer1 = wx.BoxSizer(wx.HORIZONTAL)
        H_sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        H_sizer3 = wx.BoxSizer(wx.HORIZONTAL)
        
        V_sizer1 = wx.BoxSizer(wx.VERTICAL)
        V_sizer5 = wx.BoxSizer(wx.VERTICAL)
               
        V_sizer1.Add(sc_text)
        V_sizer1.Add(sc)
        H_sizer3.Add(self.time_ctrl)
        H_sizer3.Add(self.spin)
        
        H_sizer1.Add(rb1, flag=wx.ALIGN_TOP|wx.ALL, border=20)
        H_sizer1.Add(rb2, flag=wx.ALIGN_TOP|wx.ALL, border=20)
        H_sizer1.Add(rb3, flag=wx.ALIGN_TOP|wx.ALL, border=20)
        H_sizer1.Add(V_sizer1, flag=wx.ALIGN_TOP|wx.ALL, border=20)
        H_sizer1.Add(cal, flag=wx.ALIGN_TOP|wx.ALL, border=25)
        H_sizer1.Add(H_sizer3, flag=wx.ALIGN_TOP|wx.ALL, border=25)
        
        H_sizer2.Add(btn_ok, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        H_sizer2.Add(btn_cancel, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        #line = wx.StaticLine(self, -1, size = (20,-1), style = wx.LI_HORIZONTAL)

        V_sizer5.Add(item_txt1, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 10)
        V_sizer5.Add(item_txt2, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 10)
        V_sizer5.Add(H_sizer1, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        V_sizer5.Add(H_sizer2, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        
        self.SetSizer(V_sizer5)
 
        self.SetAutoLayout(True)
            
        self.Layout() #doesn't appear necessary
        
        if period=='week':
            H_sizer1.Show(2, True)
            H_sizer1.Show(3, False)
            H_sizer1.Show(4, False)
        elif period=='month':
            H_sizer1.Show(2, False)
            H_sizer1.Show(3, True)                        
            H_sizer1.Show(4, False)
        elif period=='year':
            H_sizer1.Show(2, False)
            H_sizer1.Show(3, False)
            H_sizer1.Show(4, True)
        else:
            H_sizer1.Show(2, False)
            H_sizer1.Show(3, False)
            H_sizer1.Show(4, False)
          
        self.SetSizer(V_sizer5) 
        self.SetAutoLayout(True)            
        self.Layout() #doesn't appear necessary
        ############################################################################################## 
        
        ###################### INSTANCE VARIABLES ####################################################
               
        self.cal = cal
        self.H_sizer1 = H_sizer1
        self.sc = sc
                           
        self.method = method
        self.period = period
        self.rdate = rdate
        self.rtime = rtime
    
    def EvtRadioBox1(self, evt=None):
        self.method = self.method_list[evt.GetInt()]
        print self.method
        
    def EvtRadioBox2(self, evt=None):
        period = self.period_list[evt.GetInt()]

        if period=='day':
            self.H_sizer1.Show(2, False)
            self.H_sizer1.Show(3, False)
            self.H_sizer1.Show(4, False)
            self.H_sizer1.Layout()
        elif period=='week':
            self.H_sizer1.Show(2, True)
            self.H_sizer1.Show(3, False)
            self.H_sizer1.Show(4, False)
            self.H_sizer1.Layout()
        elif period=='month':
            self.H_sizer1.Show(2, False)
            self.H_sizer1.Show(3, True)
            self.H_sizer1.Show(4, False)
            self.H_sizer1.Layout()
        else:
            self.H_sizer1.Show(2, False)
            self.H_sizer1.Show(3, False)
            self.H_sizer1.Show(4, True)
            self.H_sizer1.Layout()
            date = datetime.datetime.today()
            rdate = ''
            for x in (date.month, date.day):
                x = str(x)
                if len(x) < 2:
                    x = '0'+x
                rdate = rdate+x
    
            self.rdate = rdate
            
            
        self.period = period
        
    def EvtRadioBox3(self, evt=None):
        self.rdate = str(evt.GetInt())
        
    def OnCalSelected(self, evt=None):
        print "Got here"
        date = evt.GetDate()
        date = datetime.datetime.fromtimestamp(date.GetTicks())
        
        rdate = ''
        for x in (date.month, date.day):
            x = str(x)
            if len(x) < 2:
                x = '0'+x
            rdate = rdate+x
    
        self.rdate = rdate
        print rdate
        
    def OnChangeMonth(self, evt=None):
        pass
        
    def OnSpin(self, evt):
        pos = evt.GetPosition()
        self.time_ctrl.SetValue(self.hour_list[pos])
        self.rtime = self.rtime_list[pos]
        
    def OnChangeTime(self, evt=None):
        pass
        #print evt.GetString()
        #self.time = evt.GetString()
        
    def EvtSpincCtrl(self, evt=None):
        # Day of Month
        self.rdate = str(evt.GetInt())
        
    def GetDate(self):
        return self.rdate
        
        
        
    
